
$(function(){
	$("#doRecharge").on("click",doRecharge);
	$("#rechargeNum").on("change",setRechargeValues);
});

function setRechargeValues(){
	var rechargeNum = $('input[name="rechargeNum"]').val();
	rechargeNum = Number(rechargeNum);
	var reg = /^([1-9]\d*\.\d*|0\.\d+|[1-9]\d*|0)$/;
			   
	if(rechargeNum == '' || reg.test(rechargeNum) == false) {
		$('#rechargeNumber').text(0);
		BSTYUN.dialog.ok('请输入正确的金额！');
		return false;
	}
	rechargeNum = parseFloat(rechargeNum).toFixed(2);
	
	$('#rechargeNumber').text(rechargeNum);
	return true;
};

function doRecharge(){
	
	var userId = $('input[name="userId"]').val();
	var rechargeNum = $('input[name="rechargeNum"]').val();
	rechargeNum = Number(rechargeNum);
	var reg = /^([1-9]\d*\.\d*|0\.\d+|[1-9]\d*|0)$/;
			   
	if(rechargeNum == '' || reg.test(rechargeNum) == false) {
		BSTYUN.dialog.ok('请输入正确的金额！');
		return;
	}
	rechargeNum = parseFloat(rechargeNum).toFixed(2);
	
	var $alipayRadio = $("#alipayRadio");
	var $chinapayRadio = $("#chinapayRadio");
	var $chinapayB2BRadio = $("#chinapayB2BRadio");
	var $chinapayB2CRadio = $("#chinapayB2CRadio");
	
	if($alipayRadio.prop("checked")){ //修改user余额   增加deal表
		location.href="/alipay/doRecharge?userId="+userId+"&rechargeNum="+rechargeNum;
	}else if($chinapayRadio.prop("checked")){
		location.href="/chinapay/rechargeByNoCard?userId="+userId+"&rechargeNum="+rechargeNum;
	
	}
	else if($chinapayB2BRadio.prop("checked")){
		location.href="/chinapay/rechargeByB2B?userId="+userId+"&rechargeNum="+rechargeNum;
	
	}else if($chinapayB2CRadio.prop("checked")){
		location.href="/chinapay/rechargeByB2C?userId="+userId+"&rechargeNum="+rechargeNum;
	
	}
	
}

//判断邮箱
var isEmail = function(str) {
        var emailReg = /^[a-z0-9][\w\.]*@([a-z0-9][a-z0-9-]*\.)+[a-z]{2,5}$/i;
        return emailReg.test(str);
    }

var isMobile = function(str){
	var mobileReg = /^1[345678][0-9]{9}$/;
	return mobileReg.test(str);
}


$(function(argument) {
	var $next = $("#next"),
		$email = $(".email"),
		$company = $(".company"),
		$rechargeNumber = $("#rechargeNumber"),
		$linkMan = $(".linkMan"),
		$linkPhone = $(".linkPhone"),
		$panel = $(".panel"),
		$back = $("#back"),
		$submit = $("#submit");
	$next.add($back).on("click",function(){
		$panel.toggle();
	});
	$submit.on('click', function(event) {
		setRechargeValues();
		var email = $.trim($email.val()),
			company = $.trim($company.val()),
			rechargeNumber = $.trim($rechargeNumber[0].innerHTML),
			linkMan = $.trim($linkMan.val()),
			linkPhone = $.trim($linkPhone.val()),
			length = company.length;
		if (length == 0) {
			$('.domain_error').eq(0).html('单位全称不能为空!');
			$('.domain_error').eq(1).html('');
			$('.domain_error').eq(2).html('');
			$('.domain_error').eq(3).html('');
			return false;
		}else{
			
		}
		
		lm_length = linkMan.length;
		if (lm_length == 0) {
			$('.domain_error').eq(0).html('');
			$('.domain_error').eq(1).html('联系人不能为空');
			$('.domain_error').eq(2).html('');
			$('.domain_error').eq(3).html('');
			return false;
		}else{
			
		}
		
		if (isMobile(linkPhone)) {
			
		}else{
			$('.domain_error').eq(0).html('');
			$('.domain_error').eq(1).html('');
			$('.domain_error').eq(2).html('手机号码格式错误');
			$('.domain_error').eq(3).html('');
			return false;
		}
		
		
		if (isEmail(email)) {

		}else{
			$('.domain_error').eq(0).html('');
			$('.domain_error').eq(1).html('');
			$('.domain_error').eq(2).html('');
			$('.domain_error').eq(3).html('邮箱格式错误');
			return false;
		}
		//发送邮件给客户    
		$.ajax({
			type:"POST",
			url:"/finance/rechargeSendEmailToUser",
			async:false,
			data:{
				email:email,
				company:company,
				rechargeNumber:rechargeNumber,
				linkMan:linkMan,
				linkPhone:linkPhone
			},
			success:function(data,response){
				if(data){
					BSTYUN.dialog.ok("您的资料已提交，请按照提交内容进行付款，款到后次日五点前将充值到您的现金账户余额，谢谢！<br>如有疑问请拨打我们的服务电话：400-8078-100",function(){
						location.href ="/finance/trade";
					});
				}else{
					BSTYUN.dialog.warn("邮件发送失败，请重新发送！");
				}
			}
		})
	});

});